<?php

namespace app\admin\controller;


class Version extends Base
{
    public function index()
    {
        $version = \app\common\model\Version::where('status', '<>', -1)->order('id', 'desc')->paginate(3);
        return $this->fetch('', [
            'version' => $version,
        ]);
    }

    /**
     * 添加功能
     * @return mixed|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input('param.');
            $validate = validate('Version');

            if (!$validate->check($data)) {
                return $this->error($validate->getError());
            }

            //入库操作
            try {
                $id = model('Version')->add($data);
            } catch (\Exception $e) {
                return $this->result('', 0, $this->error($e->getMessage()));
            }

            if ($id) {
                return $this->result(['jump_url' => url('version/index')], 1, 'OK');
            } else {
                return $this->result('', 0, '新增失败');
            }
        } else {
            $cats = model('Versioncate')->where('status', '<>', -1)->select();
            return $this->fetch('', [
                'cats' => $cats,
            ]);
        }
    }

    /**
     * 修改
     * @return mixed|void
     */
    public function edit()
    {
        if (request()->isPost()) {
            $data = input('param.');
            $post = input('post.');
            $validate = validate('Version');
            if (!$validate->check($data)) {
                return $this->error($validate->getError());
            }

            try {
                $user = model('Version')->get(['id' => $data['id']]);
            } catch (\Exception $e) {
                return $this->result('', 0, $e->getMessage());
            }

            try {
                $res = model('Version')->save($post, ['id' => $data['id']]);
            } catch (\Exception $e) {
                return $this->result('', 0, $e->getMessage());
            }

            if ($res) {
                return $this->result(['jump_url' => url('Version/index')], 1, 'OK');
            }
            return $this->result('', 0, '编辑失败');

        } else {
            try {
                $id = input('param.');
                $data = model('Version')->get(['id' => $id['id']]);
            } catch (\Exception $e) {
                return $this->result('', 0, $this->error($e->getMessage()));
            }
            try {
                $cats = model('Versioncate')->where('status', '<>', -1)->select();
            } catch (\Exception $e) {
                return $this->result('', 0, $this->error($e->getMessage()));
            }

            return $this->fetch('', [
                'cats' => $cats,
                'data' => $data,
            ]);
        }
    }


}
